<template>
  <view v-if="shenheStatus == 0" class="exchange-card">
    <u-form
      :model="form"
      ref="uForm"
      labelAlign="center"
      :borderBottom="true"
      label-width="184rpx"
    >
      <u-form-item :label="$t('user.cardAcc')">
        <u-input v-model="form.account" :placeholder="$t('user.noCardAcc')" />
      </u-form-item>
      <u-form-item :label="$t('user.cardPass')">
        <u-input v-model="form.cardpwd" :placeholder="$t('user.noCardPadd')" />
      </u-form-item>
    </u-form>

    <view class="submit-bt" @click="onSubmit">{{ $t('user.view') }}</view>
  </view>
  <Shenhe v-else-if="shenheStatus == 1"></Shenhe>
</template>

<script>
import Shenhe from '@/components/shenhe/shenhe';
export default {
  components: { Shenhe },
  data() {
    return {
      form: {
        account: '',
        cardpwd: '',
      },
      rules: {
        username: [
          {
            required: true,
            message: this.$t('user.noCardAcc'),
            trigger: ['blur', 'change'],
          },
        ],
        password: [
          {
            required: true,
            message: this.$t('user.noCardPadd'),
            trigger: ['blur', 'change'],
          },
        ],
      },
      card_id: '',
      cardData: '',
    };
  },
  methods: {
    async onSubmit() {
      try {
        if (!this.form.account.length) throw this.$t('user.noAcc');
        if (!this.form.cardpwd.length) throw this.$t('user.noPass');
        // 验证卡密
        if (
          this.form.account === this.cardData.card_info.user &&
          this.form.cardpwd === this.cardData.card_info.password
        ) {
          // 礼品卡
          if (this.cardData.verify_card_info.type == 2) {
            this.$utils.toUrl(
              '/user/write_off_card/card-list/shop-card?id=' + this.cardData.id
            );
          }
          // 储值卡
          else if (this.cardData.verify_card_info.type == 3) {
            const res = await this.$allrequest.coupon.verifyExchange({
              sale_id: this.cardData.id,
            });
            if (res.code != 0) throw res.msg || this.$t('user.adminErr');
            uni.showToast({
              title: this.$t('integral.success'),
              duration: 1500,
            });
            setTimeout(() => {
              this.$utils.toUrl('/user/balance/balance');
              W;
            }, 1500);
          }
          // 虚拟卡
          else if (this.cardData.verify_card_info.type == 4) {
          }
          // 视频卡
          else if (this.cardData.verify_card_info.type == 5) {
            this.$utils.toUrl(
              '/user/write_off_card/card-list/video-card?id=' + this.cardData.id
            );
          }
        } else {
          this.$utils.toast(this.$t('user.paddErr'));
        }
      } catch (error) {
        this.$utils.toast(error);
      }
    },
    // 获取数据详情
    async getDetail() {
      const { data } = await this.$allrequest.coupon.verifyDetail(
        {
          sale_id: this.card_id,
        },
        true
      );
      this.cardData = data.list;
      this.form.account = data.list.card_info.user;
    },
  },
  onLoad(params) {
    this.card_id = params?.id;

    this.getDetail();
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
};
</script>

<style>
.submit-bt {
  margin: 36rpx auto 0;
  width: 678rpx;
  height: 88rpx;
  line-height: 88rpx;
  text-align: center;
  background: #f0250e;
  opacity: 1;
  border-radius: 120rpx;
  font-size: 32rpx;
  color: #ffffff;
}
.exchange-card {
  padding: 0 20rpx;
}
</style>
